<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>自制深度学习框架--计算图中表达式的解析 | kiloGrand</title><meta name="keywords" content="kuiper_infer"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="本文目标在PNNX的Expession Layer中给出的是一种抽象表达式，会对计算过程进行折叠，消除中间变量，并且将具体的输入张量替换为抽象输入@0, @1等。PNNX生成的抽象表达式是这样的：12add(@0,mul(@1,@2))add(add(mul(@0,@1),mul(@2,add(add(add(@0,@2),@3),@4))),@5)这就要求我们需要一个表达式解析和语法树构建的功能">
<meta property="og:type" content="article">
<meta property="og:title" content="自制深度学习框架--计算图中表达式的解析">
<meta property="og:url" content="https://kilogrand.gitee.io/2023/03/18/kuiper_infer-L8/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="本文目标在PNNX的Expession Layer中给出的是一种抽象表达式，会对计算过程进行折叠，消除中间变量，并且将具体的输入张量替换为抽象输入@0, @1等。PNNX生成的抽象表达式是这样的：12add(@0,mul(@1,@2))add(add(mul(@0,@1),mul(@2,add(add(add(@0,@2),@3),@4))),@5)这就要求我们需要一个表达式解析和语法树构建的功能">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2023-03-18T12:00:00.000Z">
<meta property="article:modified_time" content="2023-04-16T02:05:00.373Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="kuiper_infer">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2023/03/18/kuiper_infer-L8/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '自制深度学习框架--计算图中表达式的解析',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-04-16 10:05:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">自制深度学习框架--计算图中表达式的解析</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-03-18T12:00:00.000Z" title="发表于 2023-03-18 20:00:00">2023-03-18</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-04-16T02:05:00.373Z" title="更新于 2023-04-16 10:05:00">2023-04-16</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0/">深度学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">2.1k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>9分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="自制深度学习框架--计算图中表达式的解析"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="本文目标"><a href="#本文目标" class="headerlink" title="本文目标"></a>本文目标</h2><p>在<code>PNNX</code>的<code>Expession Layer</code>中给出的是一种抽象表达式，会对计算过程进行折叠，消除中间变量，<br>并且将具体的输入张量替换为抽象输入@0, @1等。PNNX生成的抽象表达式是这样的：<br><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">add(@0,mul(@1,@2))</span><br><span class="line">add(add(mul(@0,@1),mul(@2,add(add(add(@0,@2),@3),@4))),@5)</span><br></pre></td></tr></table></figure><br>这就要求我们需要一个<code>表达式解析</code>和<code>语法树构建</code>的功能。</p>
<h2 id="词法解析和语法解析"><a href="#词法解析和语法解析" class="headerlink" title="词法解析和语法解析"></a>词法解析和语法解析</h2><p>词法解析的目的：将add(@0,mul(@1,@2))拆分为多个token，如下所示：<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">[add, left bracket, mul, left bracket, @<span class="number">0</span>, comma, @<span class="number">1</span>, right bracket, @<span class="number">2</span>, right bracket]</span><br></pre></td></tr></table></figure></p>
<p>语法解析的目的：当得到token数组之后，我们对语法进行分析，构建抽象语法树。</p>
<p>首先，我们在TokenType中规定了Token的类型，类型有输入、加法、乘法以及左右括号等。<br>然后，在Token类中记录了类型以及Token在字符串的起始和结束位置。<br>抽象语法树由一个二叉树组成，在TokenNode类中，存储左子树和右子树的指针，<br>num_index为正, 则表明是输入的编号，例如@0,@1中的num_index依次为1和2。<br>如果num_index为负数则表明当前的节点是mul或者add等operator。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum class</span> <span class="title class_">TokenType</span> &#123;</span><br><span class="line">  TokenUnknown = <span class="number">-1</span>,</span><br><span class="line">  TokenInputNumber = <span class="number">0</span>,</span><br><span class="line">  TokenComma = <span class="number">1</span>,</span><br><span class="line">  TokenAdd = <span class="number">2</span>,</span><br><span class="line">  TokenMul = <span class="number">3</span>,</span><br><span class="line">  TokenLeftBracket = <span class="number">4</span>,</span><br><span class="line">  TokenRightBracket = <span class="number">5</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">Token</span> &#123;</span><br><span class="line">  TokenType token_type = TokenType::TokenUnknown;</span><br><span class="line">  <span class="type">int32_t</span> start_pos = <span class="number">0</span>; <span class="comment">//词语开始的位置</span></span><br><span class="line">  <span class="type">int32_t</span> end_pos = <span class="number">0</span>; <span class="comment">// 词语结束的位置</span></span><br><span class="line">  <span class="built_in">Token</span>(TokenType token_type, <span class="type">int32_t</span> start_pos, <span class="type">int32_t</span> end_pos)</span><br><span class="line">      : <span class="built_in">token_type</span>(token_type), <span class="built_in">start_pos</span>(start_pos), <span class="built_in">end_pos</span>(end_pos) &#123;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">TokenNode</span> &#123;</span><br><span class="line">  <span class="type">int32_t</span> num_index = <span class="number">-1</span>;</span><br><span class="line">  std::shared_ptr&lt;TokenNode&gt; left = <span class="literal">nullptr</span>;</span><br><span class="line">  std::shared_ptr&lt;TokenNode&gt; right = <span class="literal">nullptr</span>;</span><br><span class="line"></span><br><span class="line">  <span class="built_in">TokenNode</span>(<span class="type">int32_t</span> num_index, std::shared_ptr&lt;TokenNode&gt; left, std::shared_ptr&lt;TokenNode&gt; right);</span><br><span class="line">  <span class="built_in">TokenNode</span>() = <span class="keyword">default</span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ExpressionParser</span> &#123;</span><br><span class="line"> <span class="keyword">public</span>:</span><br><span class="line">  <span class="function"><span class="keyword">explicit</span> <span class="title">ExpressionParser</span><span class="params">(std::string statement)</span> : statement_(std::move(statement)) &#123;</span></span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 分词器，词法解析</span></span><br><span class="line">  <span class="function"><span class="type">void</span> <span class="title">Tokenizer</span><span class="params">(<span class="type">bool</span> need_retoken = <span class="literal">false</span>)</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 语法解析，生成抽象语法树，并返回逆波兰表达式</span></span><br><span class="line">  std::vector&lt;std::shared_ptr&lt;TokenNode&gt;&gt; <span class="built_in">Generate</span>();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 返回token容器</span></span><br><span class="line">  <span class="function"><span class="type">const</span> std::vector&lt;Token&gt; &amp;<span class="title">tokens</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 返回token对应的字符串容器</span></span><br><span class="line">  <span class="function"><span class="type">const</span> std::vector&lt;std::string&gt; &amp;<span class="title">token_strs</span><span class="params">()</span> <span class="type">const</span></span>;</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span>:</span><br><span class="line">  <span class="function">std::shared_ptr&lt;TokenNode&gt; <span class="title">Generate_</span><span class="params">(<span class="type">int32_t</span> &amp;index)</span></span>;</span><br><span class="line">  std::vector&lt;Token&gt; tokens_;  <span class="comment">// 存储分词后的token类容器</span></span><br><span class="line">  std::vector&lt;std::string&gt; token_strs_;  <span class="comment">// 存储分词后的token对于的字符串容器</span></span><br><span class="line">  std::string statement_;  <span class="comment">// 存放输入 </span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="分词器Tokenizer的实现"><a href="#分词器Tokenizer的实现" class="headerlink" title="分词器Tokenizer的实现"></a>分词器Tokenizer的实现</h2><ol>
<li>我们将输入存放在statement<em>中，首先是判断statement</em>是否为空，随后删除表达式中的所有空格和制表符。</li>
<li>遍历表达式，判断当前字符<ul>
<li>当c等于字符a的时候,我们的词法规定在token中以a作为开始的情况只有add. 所以我们判断接下来的两个字符必须是d和 d.如果不是的话就报错,如果是i的话就初始化一个新的token并进行保存.</li>
<li>当c等于字符m的时候,我们的语法规定token中以m作为开始的情况只有mul. 所以我们判断接下来的两个字必须是u和l. 如果不是的话,就报错,是的话就初始化一个token进行保存mul.</li>
</ul>
</li>
<li>保存每一个token到tokens<em>容器以及对应的字符串到token_strs</em>容器</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">ExpressionParser::Tokenizer</span><span class="params">(<span class="type">bool</span> need_retoken)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!need_retoken &amp;&amp; !<span class="keyword">this</span>-&gt;tokens_.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="keyword">return</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 删除表达式中的所有空格和制表符</span></span><br><span class="line">  <span class="built_in">CHECK</span>(!statement_.<span class="built_in">empty</span>()) &lt;&lt; <span class="string">&quot;The input statement is empty!&quot;</span>;</span><br><span class="line">  statement_.<span class="built_in">erase</span>(std::<span class="built_in">remove_if</span>(statement_.<span class="built_in">begin</span>(), statement_.<span class="built_in">end</span>(), [](<span class="type">char</span> c) &#123;</span><br><span class="line">    <span class="keyword">return</span> std::<span class="built_in">isspace</span>(c);</span><br><span class="line">  &#125;), statement_.<span class="built_in">end</span>());</span><br><span class="line">  <span class="built_in">CHECK</span>(!statement_.<span class="built_in">empty</span>()) &lt;&lt; <span class="string">&quot;The input statement is empty!&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 遍历表达式</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int32_t</span> i = <span class="number">0</span>; i &lt; statement_.<span class="built_in">size</span>();) &#123;</span><br><span class="line">    <span class="type">char</span> c = statement_.<span class="built_in">at</span>(i);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (c == <span class="string">&#x27;a&#x27;</span>) &#123;  <span class="comment">// add</span></span><br><span class="line">      <span class="built_in">CHECK</span>(i + <span class="number">1</span> &lt; statement_.<span class="built_in">size</span>() &amp;&amp; statement_.<span class="built_in">at</span>(i + <span class="number">1</span>) == <span class="string">&#x27;d&#x27;</span>)</span><br><span class="line">              &lt;&lt; <span class="string">&quot;Parse add token failed, illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">      <span class="built_in">CHECK</span>(i + <span class="number">2</span> &lt; statement_.<span class="built_in">size</span>() &amp;&amp; statement_.<span class="built_in">at</span>(i + <span class="number">2</span>) == <span class="string">&#x27;d&#x27;</span>)</span><br><span class="line">              &lt;&lt; <span class="string">&quot;Parse add token failed, illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">      <span class="function">Token <span class="title">token</span><span class="params">(TokenType::TokenAdd, i, i + <span class="number">3</span>)</span></span>;</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_operation = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + i + <span class="number">3</span>);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_operation);</span><br><span class="line">      i = i + <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;m&#x27;</span>) &#123;  <span class="comment">// mul</span></span><br><span class="line">      <span class="built_in">CHECK</span>(i + <span class="number">1</span> &lt; statement_.<span class="built_in">size</span>() &amp;&amp; statement_.<span class="built_in">at</span>(i + <span class="number">1</span>) == <span class="string">&#x27;u&#x27;</span>)</span><br><span class="line">              &lt;&lt; <span class="string">&quot;Parse add token failed, illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">      <span class="built_in">CHECK</span>(i + <span class="number">2</span> &lt; statement_.<span class="built_in">size</span>() &amp;&amp; statement_.<span class="built_in">at</span>(i + <span class="number">2</span>) == <span class="string">&#x27;l&#x27;</span>)</span><br><span class="line">              &lt;&lt; <span class="string">&quot;Parse add token failed, illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">      <span class="function">Token <span class="title">token</span><span class="params">(TokenType::TokenMul, i, i + <span class="number">3</span>)</span></span>;</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_operation = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + i + <span class="number">3</span>);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_operation);</span><br><span class="line">      i = i + <span class="number">3</span>;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;@&#x27;</span>) &#123; <span class="comment">// @num</span></span><br><span class="line">      <span class="built_in">CHECK</span>(i + <span class="number">1</span> &lt; statement_.<span class="built_in">size</span>() &amp;&amp; std::<span class="built_in">isdigit</span>(statement_.<span class="built_in">at</span>(i + <span class="number">1</span>)))</span><br><span class="line">              &lt;&lt; <span class="string">&quot;Parse number token failed, illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">      <span class="type">int32_t</span> j = i + <span class="number">1</span>;</span><br><span class="line">      <span class="keyword">for</span> (; j &lt; statement_.<span class="built_in">size</span>(); ++j) &#123;</span><br><span class="line">        <span class="keyword">if</span> (!std::<span class="built_in">isdigit</span>(statement_.<span class="built_in">at</span>(j))) &#123;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="function">Token <span class="title">token</span><span class="params">(TokenType::TokenInputNumber, i, j)</span></span>;</span><br><span class="line">      <span class="built_in">CHECK</span>(token.start_pos &lt; token.end_pos);</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_input_number = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + j);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_input_number);</span><br><span class="line">      i = j;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;,&#x27;</span>) &#123;</span><br><span class="line">      Token <span class="built_in">token</span>(TokenType::TokenComma, i, i + <span class="number">1</span>);</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_comma = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + i + <span class="number">1</span>);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_comma);</span><br><span class="line">      i += <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;(&#x27;</span>) &#123;</span><br><span class="line">      Token <span class="built_in">token</span>(TokenType::TokenLeftBracket, i, i + <span class="number">1</span>);</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_left_bracket = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + i + <span class="number">1</span>);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_left_bracket);</span><br><span class="line">      i += <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (c == <span class="string">&#x27;)&#x27;</span>) &#123;</span><br><span class="line">      Token <span class="built_in">token</span>(TokenType::TokenRightBracket, i, i + <span class="number">1</span>);</span><br><span class="line">      tokens_.<span class="built_in">push_back</span>(token);</span><br><span class="line">      std::string token_right_bracket = std::<span class="built_in">string</span>(statement_.<span class="built_in">begin</span>() + i, statement_.<span class="built_in">begin</span>() + i + <span class="number">1</span>);</span><br><span class="line">      token_strs_.<span class="built_in">push_back</span>(token_right_bracket);</span><br><span class="line">      i += <span class="number">1</span>;</span><br><span class="line"></span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">LOG</span>(FATAL) &lt;&lt; <span class="string">&quot;Unknown  illegal character: &quot;</span> &lt;&lt; c;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同时，并未分词后的结果定义了两个接口，用于获取。<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">const</span> std::vector&lt;Token&gt; &amp;<span class="title">ExpressionParser::tokens</span><span class="params">()</span> <span class="type">const</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;tokens_;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">const</span> std::vector&lt;std::string&gt; &amp;<span class="title">ExpressionParser::token_strs</span><span class="params">()</span> <span class="type">const</span> </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">this</span>-&gt;token_strs_;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="抽象语法树的生成"><a href="#抽象语法树的生成" class="headerlink" title="抽象语法树的生成"></a>抽象语法树的生成</h2><p>我们定义Generate<em>函数来生成抽象语法树，并返回根节点，传入index来记录遍历了多少个token。<br>步骤如下：<br>遍历tokens</em>容器中的每一个token</p>
<pre><code>- 如果当前Token类型是数字，那么需要创建并返回TokenNode
- 如果当前Token类型是mul或者add，那么需要向下递归构建对应的左子树和右子树
    - 1，判断是不是“(”
    - 2，获取@num
    - 3，递归调用自身，创建左子树
    - 4，判断是不是“,”
    - 5，获取@num
    - 6，递归调用自身，创建右子树
    - 7，判断是不是“)”
</code></pre><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 初始化语法树中的节点</span></span><br><span class="line">TokenNode::<span class="built_in">TokenNode</span>(<span class="type">int32_t</span> num_index, std::shared_ptr&lt;TokenNode&gt; left, std::shared_ptr&lt;TokenNode&gt; right) :</span><br><span class="line">    <span class="built_in">num_index</span>(num_index), <span class="built_in">left</span>(std::<span class="built_in">move</span>(left)), <span class="built_in">right</span>(std::<span class="built_in">move</span>(right)) &#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 递归创建语法树</span></span><br><span class="line"><span class="function">std::shared_ptr&lt;TokenNode&gt; <span class="title">ExpressionParser::Generate_</span><span class="params">(<span class="type">int32_t</span> &amp;index)</span> </span>&#123;</span><br><span class="line">  <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">  <span class="type">const</span> <span class="keyword">auto</span> current_token = <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index);</span><br><span class="line">  <span class="built_in">CHECK</span>(current_token.token_type == TokenType::TokenInputNumber</span><br><span class="line">            || current_token.token_type == TokenType::TokenAdd || current_token.token_type == TokenType::TokenMul);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (current_token.token_type == TokenType::TokenInputNumber) &#123;</span><br><span class="line">    <span class="comment">// 如果当前Token类型是数字，那么需要创建并返回TokenNode</span></span><br><span class="line">    <span class="type">uint32_t</span> start_pos = current_token.start_pos + <span class="number">1</span>;</span><br><span class="line">    <span class="type">uint32_t</span> end_pos = current_token.end_pos;</span><br><span class="line">    <span class="built_in">CHECK</span>(end_pos &gt; start_pos);</span><br><span class="line">    <span class="built_in">CHECK</span>(end_pos &lt;= <span class="keyword">this</span>-&gt;statement_.<span class="built_in">length</span>());</span><br><span class="line"></span><br><span class="line">    <span class="type">const</span> std::string &amp;str_number =</span><br><span class="line">        std::<span class="built_in">string</span>(<span class="keyword">this</span>-&gt;statement_.<span class="built_in">begin</span>() + start_pos, <span class="keyword">this</span>-&gt;statement_.<span class="built_in">begin</span>() + end_pos);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> std::<span class="built_in">make_shared</span>&lt;TokenNode&gt;(std::<span class="built_in">stoi</span>(str_number), <span class="literal">nullptr</span>, <span class="literal">nullptr</span>);</span><br><span class="line"></span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span> (current_token.token_type == TokenType::TokenMul || current_token.token_type == TokenType::TokenAdd) &#123;</span><br><span class="line">    <span class="comment">// 如果当前Token类型是mul或者add，那么需要向下递归构建对应的左子节点和右子节点.</span></span><br><span class="line">    std::shared_ptr&lt;TokenNode&gt; current_node = std::<span class="built_in">make_shared</span>&lt;TokenNode&gt;();</span><br><span class="line">    current_node-&gt;num_index = -<span class="built_in">int</span>(current_token.token_type);</span><br><span class="line">    <span class="comment">// 判断是不是“(”</span></span><br><span class="line">    index += <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">    <span class="built_in">CHECK</span>(<span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index).token_type == TokenType::TokenLeftBracket);</span><br><span class="line">    <span class="comment">// 获取@num</span></span><br><span class="line">    index += <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">    <span class="type">const</span> <span class="keyword">auto</span> left_token = <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index);</span><br><span class="line">    <span class="comment">// 递归创建左子树</span></span><br><span class="line">    <span class="keyword">if</span> (left_token.token_type == TokenType::TokenInputNumber</span><br><span class="line">        || left_token.token_type == TokenType::TokenAdd || left_token.token_type == TokenType::TokenMul) &#123;</span><br><span class="line">      current_node-&gt;left = <span class="built_in">Generate_</span>(index);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">LOG</span>(FATAL) &lt;&lt; <span class="string">&quot;Unknown token type: &quot;</span> &lt;&lt; <span class="built_in">int</span>(left_token.token_type);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断是不是“,”</span></span><br><span class="line">    index += <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">    <span class="built_in">CHECK</span>(<span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index).token_type == TokenType::TokenComma);</span><br><span class="line">    <span class="comment">// 获取@num</span></span><br><span class="line">    index += <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">    <span class="type">const</span> <span class="keyword">auto</span> right_token = <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index);</span><br><span class="line">    <span class="comment">// 递归创建右子树</span></span><br><span class="line">    <span class="keyword">if</span> (right_token.token_type == TokenType::TokenInputNumber</span><br><span class="line">        || right_token.token_type == TokenType::TokenAdd || right_token.token_type == TokenType::TokenMul) &#123;</span><br><span class="line">      current_node-&gt;right = <span class="built_in">Generate_</span>(index);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      <span class="built_in">LOG</span>(FATAL) &lt;&lt; <span class="string">&quot;Unknown token type: &quot;</span> &lt;&lt; <span class="built_in">int</span>(left_token.token_type);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 判断是不是“)”</span></span><br><span class="line">    index += <span class="number">1</span>;</span><br><span class="line">    <span class="built_in">CHECK</span>(index &lt; <span class="keyword">this</span>-&gt;tokens_.<span class="built_in">size</span>());</span><br><span class="line">    <span class="built_in">CHECK</span>(<span class="keyword">this</span>-&gt;tokens_.<span class="built_in">at</span>(index).token_type == TokenType::TokenRightBracket);</span><br><span class="line">    <span class="keyword">return</span> current_node;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="built_in">LOG</span>(FATAL) &lt;&lt; <span class="string">&quot;Unknown token type: &quot;</span> &lt;&lt; <span class="built_in">int</span>(current_token.token_type);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="逆波兰表达式"><a href="#逆波兰表达式" class="headerlink" title="逆波兰表达式"></a>逆波兰表达式</h2><p>后序遍历抽象语法树，就可以得到逆波兰表达式<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">ReversePolish</span><span class="params">(<span class="type">const</span> std::shared_ptr&lt;TokenNode&gt; &amp;root_node,</span></span></span><br><span class="line"><span class="params"><span class="function">                   std::vector&lt;std::shared_ptr&lt;TokenNode&gt;&gt; &amp;reverse_polish)</span> </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (root_node != <span class="literal">nullptr</span>) &#123;</span><br><span class="line">    <span class="built_in">ReversePolish</span>(root_node-&gt;left, reverse_polish);</span><br><span class="line">    <span class="built_in">ReversePolish</span>(root_node-&gt;right, reverse_polish);</span><br><span class="line">    reverse_polish.<span class="built_in">push_back</span>(root_node);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 生成抽象语法树，并返回逆波兰表达式</span></span><br><span class="line">std::vector&lt;std::shared_ptr&lt;TokenNode&gt;&gt; ExpressionParser::<span class="built_in">Generate</span>() &#123;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span> (<span class="keyword">this</span>-&gt;tokens_.<span class="built_in">empty</span>()) &#123;</span><br><span class="line">    <span class="keyword">this</span>-&gt;<span class="built_in">Tokenizer</span>(<span class="literal">true</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="type">int</span> index = <span class="number">0</span>;</span><br><span class="line">  <span class="comment">// 生成抽象语法树</span></span><br><span class="line">  std::shared_ptr&lt;TokenNode&gt; root = <span class="built_in">Generate_</span>(index);</span><br><span class="line">  <span class="built_in">CHECK</span>(root != <span class="literal">nullptr</span>);  <span class="comment">// 判断根节点是否非空</span></span><br><span class="line">  <span class="built_in">CHECK</span>(index == tokens_.<span class="built_in">size</span>() - <span class="number">1</span>);  <span class="comment">// 判断是否已经遍历了每一个token</span></span><br><span class="line"></span><br><span class="line">  std::vector&lt;std::shared_ptr&lt;TokenNode&gt;&gt; reverse_polish;</span><br><span class="line">  <span class="built_in">ReversePolish</span>(root, reverse_polish);</span><br><span class="line">  <span class="keyword">return</span> reverse_polish;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2023/03/18/kuiper_infer-L8/">https://kilogrand.gitee.io/2023/03/18/kuiper_infer-L8/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/kuiper-infer/">kuiper_infer</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/03/17/kuiper_infer-L7/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">自制深度学习框架--构建自己的计算图</div></div></a></div><div class="next-post pull-right"><a href="/2023/03/19/kuiper_infer-L9/"><img class="next-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">自制深度学习框架--实现表达式层ExpressionLayer</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-20</div><div class="title">自制深度学习框架--Im2Col原理与卷积层的实现</div></div></a></div><div><a href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-21</div><div class="title">自制深度学习框架--再探Tensor类并构建计算图的图关系</div></div></a></div><div><a href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-24</div><div class="title">自制深度学习框架--实现Yolov5的推理</div></div></a></div><div><a href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-22</div><div class="title">自制深度学习框架--算子的执行流程</div></div></a></div><div><a href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-23</div><div class="title">自制深度学习框架--实现ResNet网络的推理</div></div></a></div><div><a href="/2023/03/14/kuiper_infer-L3/" title="自制深度学习框架--导入数据"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-03-14</div><div class="title">自制深度学习框架--导入数据</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%AC%E6%96%87%E7%9B%AE%E6%A0%87"><span class="toc-number">1.</span> <span class="toc-text">本文目标</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%8D%E6%B3%95%E8%A7%A3%E6%9E%90%E5%92%8C%E8%AF%AD%E6%B3%95%E8%A7%A3%E6%9E%90"><span class="toc-number">2.</span> <span class="toc-text">词法解析和语法解析</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E8%AF%8D%E5%99%A8Tokenizer%E7%9A%84%E5%AE%9E%E7%8E%B0"><span class="toc-number">3.</span> <span class="toc-text">分词器Tokenizer的实现</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8A%BD%E8%B1%A1%E8%AF%AD%E6%B3%95%E6%A0%91%E7%9A%84%E7%94%9F%E6%88%90"><span class="toc-number">4.</span> <span class="toc-text">抽象语法树的生成</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%86%E6%B3%A2%E5%85%B0%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">5.</span> <span class="toc-text">逆波兰表达式</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>